#include<iostream>
#include<stack>
#include<queue>
using namespace std;

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		double n;
		int k;
		cin >> n >> k;
		if (n == 0) {
			cout << "0.000" << endl;
			continue;
		}
		stack<int>A1;
		queue<int>A2;
		int n1 = int(n);
		double n2 = n - n1;
		while (n1) {
			A1.push(n1 % k);
			n1 /= k;
		}
		int l = 0;
		while (l<3) {
			n2 *= k;
			A2.push((int)n2);
			n2 -= int(n2);
			l++;
		}

		while (!A1.empty()) {
			int m = A1.top();
			if (m < 10) {
				cout << m;
			}
			else {
				if (m == 10) {
					cout << 'A';
				}
				else if (m == 11) {
					cout << 'B';
				}
				else if (m == 12) {
					cout << 'C';
				}
				else if (m == 13) {
					cout << 'D';
				}
				else if (m == 14) {
					cout << 'E';
				}
				else {
					cout << 'F';
				}
			}
			A1.pop();
		}
		cout << '.';
		while (!A2.empty()) {
			int m = A2.front();
			cout << m;
			A2.pop();
		}
		cout << endl;
	}
	return 0;
}
